package com.gas.project.gas.resit.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import com.gas.common.utils.DateUtils;
import com.gas.common.utils.StringUtils;
import com.gas.common.utils.poi.ExcelUtil;
import com.gas.framework.aspectj.lang.annotation.Log;
import com.gas.framework.aspectj.lang.enums.BusinessType;
import com.gas.framework.web.controller.BaseController;
import com.gas.framework.web.domain.AjaxResult;
import com.gas.framework.web.page.TableDataInfo;
import com.gas.project.gas.grade.domain.CourseGrade;
import com.gas.project.gas.message.domain.Message;
import com.gas.project.gas.message.service.IMessageService;
import com.gas.project.gas.student.domain.Student;
import com.gas.project.gas.student.service.IStudentService;
import com.gas.project.gas.teacher.domain.Teacher;
import com.gas.project.system.user.domain.User;
import com.gas.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gas.project.gas.resit.domain.CourseResit;
import com.gas.project.gas.resit.service.ICourseResitService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 补考得分Controller
 *
 * @author xyl
 * @date 2022-04-04
 */
@Controller
@RequestMapping("/gas/resit")
public class CourseResitController extends BaseController {
    private String prefix = "gas/resit";

    @Autowired
    private ICourseResitService courseResitService;
    @Autowired
    private IStudentService studentService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IMessageService messageService;

    @RequiresPermissions("gas:resit:view")
    @GetMapping()
    public String resit(ModelMap mmap) {
        User sysUser = getSysUser();
        if(sysUser.isStudent()){
            Student student = studentService.selectStudentById(sysUser.getLoginName());
            String content = "";
            long start = student.getAdmissionsTime().getTime();
            long end = System.currentTimeMillis();
            long day = (end - start) / 24 / 60 /60 / 1000;
            int year = (int)day % 180 + 1;
            CourseResit courseResit = new CourseResit();
            courseResit.setStudentNumber(sysUser.getLoginName());
            if(year < 8){
                courseResit.setStartYear(2021);
                courseResit.setSemester("下");
                List<CourseResit> list = courseResitService.selectCourseResitList(courseResit);
                List<CourseResit> jg = list.stream().filter(c -> c.getResitScore().compareTo(new BigDecimal(60)) > -1).collect(Collectors.toList());
                List<CourseResit> bjg = list.stream().filter(c -> c.getResitScore().compareTo(new BigDecimal(60)) == -1).collect(Collectors.toList());
                content = "本学期补考通过：" + jg.size() + "个，未通过：" + bjg.size() + "个";
            }else{
                List<CourseResit> list = courseResitService.selectCourseResitList(courseResit);
                List<CourseResit> jg = list.stream().filter(c -> c.getResitScore().compareTo(new BigDecimal(60)) > -1).collect(Collectors.toList());
                List<CourseResit> bjg = list.stream().filter(c -> c.getResitScore().compareTo(new BigDecimal(60)) == -1).collect(Collectors.toList());
                content = "总补考通过：" + jg.size() + "个，未通过：" + bjg.size() + "个";
            }
            mmap.put("content",content);
            mmap.put("gaa","1");

        }
        return prefix + "/resit";
    }

    /**
     * 查询补考得分列表
     */
    @RequiresPermissions("gas:resit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CourseResit courseResit) {
        User sysUser = getSysUser();
        if(sysUser.isStudent()){
            courseResit.setStudentNumber(sysUser.getLoginName());
        }
        if(sysUser.isTeacher()){
            courseResit.setTeacherNumber(sysUser.getLoginName());
        }
        startPage();
        List<CourseResit> list = courseResitService.selectCourseResitList(courseResit);
        for (CourseResit cr : list) {
            User user = userService.selectUserByLoginName(cr.getTeacherNumber());
            Message message = new Message();
            message.setModuleName("resit");
            message.setCourseNumber(cr.getCourseNumber());
            message.setSubmitId(sysUser.getUserId());
            message.setReceiveId(user.getUserId());
            List<Message> messages = messageService.selectMessageList(message);
            cr.setReply(messages.size() > 0);
        }
        return getDataTable(list);
    }

    /**
     * 导出补考得分列表
     */
    @RequiresPermissions("gas:resit:export")
    @Log(title = "补考得分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CourseResit courseResit) {
        List<CourseResit> list = courseResitService.selectCourseResitList(courseResit);
        ExcelUtil<CourseResit> util = new ExcelUtil<CourseResit>(CourseResit.class);
        return util.exportExcel(list, "resit");
    }

    /**
     * 新增补考得分
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存补考得分
     */
    @RequiresPermissions("gas:resit:add")
    @Log(title = "补考得分", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CourseResit courseResit) {
        return toAjax(courseResitService.insertCourseResit(courseResit));
    }

    /**
     * 修改补考得分
     */
    @GetMapping("/edit/{courseResitId}")
    public String edit(@PathVariable("courseResitId") Long courseResitId, ModelMap mmap) {
        CourseResit courseResit = courseResitService.selectCourseResitById(courseResitId);
        mmap.put("courseResit", courseResit);
        return prefix + "/edit";
    }

    /**
     * 修改保存补考得分
     */
    @RequiresPermissions("gas:resit:edit")
    @Log(title = "补考得分", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CourseResit courseResit) {
        return toAjax(courseResitService.updateCourseResit(courseResit));
    }

    /**
     * 删除补考得分
     */
    @RequiresPermissions("gas:resit:remove")
    @Log(title = "补考得分", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(courseResitService.deleteCourseResitByIds(ids));
    }

    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<CourseResit> util = new ExcelUtil<>(CourseResit.class);
        return util.importTemplateExcel("补考数据");
    }


    @Log(title = "补考数据" , businessType = BusinessType.IMPORT)
    @RequiresPermissions("gas:resit:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<CourseResit> util = new ExcelUtil<>(CourseResit.class);
        List<CourseResit> courseResitList = util.importExcel(file.getInputStream());
        //数据校验
        for (CourseResit courseResit : courseResitList) {
            courseResit.setCreateBy(getLoginName());
            courseResit.setCreateTime(DateUtils.getNowDate());
            String check = courseResit.check(courseResit);
            if(StringUtils.isNotEmpty(check)){
                return error(check);
            }
        }
        String message = courseResitService.importCourseResit(courseResitList);
        return AjaxResult.success(message);
    }
}
